package org.black.phoneUtility.sms.reply.app.fragment;

import java.io.Serializable;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class SmsReplyEditMessageDialogFragment extends DialogFragment {

	public static final String ARGEMENTS_MESSAGE = "message";
	public static final String ARGEMENTS_MESSAGE_FOR = "messageFor";

	public static class MessageFor implements Serializable {
		private static final long serialVersionUID = 2802800429777047290L;
		public static MessageFor ALL = new MessageFor(1024);
		public static MessageFor MISSED_CALL = new MessageFor(128);
		public static MessageFor SMS = new MessageFor(129);
		private int code;

		private MessageFor(int code) {
			this.code = code;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + code;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			MessageFor other = (MessageFor) obj;
			if (code != other.code)
				return false;
			return true;
		}
	}

	public static final String TAG = "SmsReplyEditMessageDialogFragmentTag";

	public interface SmsReplyEditMessageListener {
		public void onEditMessageDialogPositiveClick(MessageFor messageFor,
				String originalMessage, String newMessage);

		public void onEditMessageDialogNegativeClick();
	}

	private SmsReplyEditMessageListener smsReplyEditMessageListener;

	private EditText messageEditText;

	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		Bundle bundle = getArguments();
		final String originalMessage = bundle.getString(ARGEMENTS_MESSAGE);
		final MessageFor messageFor = (MessageFor) bundle
				.getSerializable(ARGEMENTS_MESSAGE_FOR);
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_add_edit_message_dialog, null);

		builder.setView(view);

		messageEditText = (EditText) view
				.findViewById(R.id.smsReplyAddEditMessageDialogMessageEditText);
		messageEditText.setText(originalMessage);
		builder.setTitle(R.string.sms_reply_add_edit_message_dialog_title)
				.setPositiveButton(android.R.string.ok, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						smsReplyEditMessageListener
								.onEditMessageDialogPositiveClick(messageFor,
										originalMessage, messageEditText
												.getText().toString());
					}
				}).setNegativeButton(android.R.string.cancel, null);
		return builder.create();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyEditMessageListener = (SmsReplyEditMessageListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement SmsReplyEditMessageListener");
		}
	}
}
